class Solution {
    public List<List<Integer>> permute(int[] nums) {
       class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res=new ArrayList<>();
        int n=nums.length;
        Deque<Integer> path=new ArrayDeque<>();
        boolean [] used=new boolean[n]; 
        dfs(n,used,0,nums,res,path);
        return res;
    }
    public void dfs(int n,boolean[] used,int depth,int nums[],List<List<Integer>> res,Deque<Integer> path)
    {
        if(depth==n){
        res.add(new ArrayList<>(path));
        return;
        }
        for(int i=0;i<n;i++)
        {
            if(used[i])
            continue;
            path.addLast(nums[i]);
            used[i]=true;
            dfs(n,used,depth+1,nums,res,path);
            path.removeLast();
            used[i]=false;
        }
    }
}




